package com.hyt.it.ogt.kq.main.controller.bm;

import javax.annotation.Resource;

import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.exception.exception.BusinessLogicException;
import com.hyt.it.ogt.kq.common.bm.swagger.BmApiVersionConstant;
import com.hyt.it.ogt.kq.common.bm.utils.Constant;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.bm.mapper.ProjectMapper;
import com.hyt.it.ogt.kq.service.bm.mapper.ProjectSubjectMapper;
import com.hyt.it.ogt.kq.service.bm.model.entity.Project;
import com.hyt.it.ogt.kq.service.bm.service.IProjectDataDownLoadRecordService;
import com.hyt.it.ogt.kq.service.bm.service.IProjectExamService;
import com.hyt.it.ogt.kq.service.bm.service.IProjectSubjectService;
import com.hyt.it.ogt.kq.service.bm.service.IStudentProjectService;
import com.hyt.it.ogt.kq.service.bm.service.ISubjectAreaLimitService;
import com.hyt.it.ogt.kq.service.gov.api.bm.BmApi;
import com.hyt.loginfo.service.IPtSecretKeyService;
import com.hyt.progress.creator.anno.ProgressAnno;
import com.hyt.progress.service.ProgressManager;
import com.hyt.swagger.ApiVersion;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 考生报名科目区域限额
 * </p>
 *
 * @since 2024-01-22
 */
@RestController
@Api(tags = "45.考生报名科目区域限额接口", value = "考生报名科目区域限额接口")
@ApiSort(value = 45)
@RequestMapping("/bm/subjectarealimit")
@Slf4j
public class SubjectAreaLimitController extends BaseController {


    @Resource
    private RedissonClient redissonClient;
    @Resource
    private ISubjectAreaLimitService iSubjectAreaLimitService;
    @Resource
    private ProjectSubjectMapper projectSubjectMapper;
    @Resource
    private IProjectExamService iProjectExamService;
    @Resource
    private BmApi kwGovClient;
    @Resource
    private IStudentProjectService iStudentProjectService;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private IProjectDataDownLoadRecordService iProjectDataDownLoadRecordService;
    @Resource
    private IProjectSubjectService iProjectSubjectService;
    @Resource
    private IPtSecretKeyService iPtSecretKeyService;


    @ApiOperation(value = "45.1.1 下载区域限额模板", notes = "20000:成功；45010101：下载区域限额模板失败")
    @ApiOperationSort(value = 1)
    @RequestMapping(value = "/downloadSubjectAreaLimitTemplate/{projectId}", method = RequestMethod.GET)
    @ThrowsAdvice(code = 31451100, print = "下载区域限额模板失败")
    @ApiVersion(group = BmApiVersionConstant.V2_4_p18)
    public ApiResponse<String> downloadSubjectAreaLimitTemplate(@PathVariable String projectId, String secretKey) throws Exception {
        log.info("# downloadSubjectAreaLimitTemplate project:{}", projectId);
        String configSecretKey = iPtSecretKeyService.getSecretKey("fjzf", "bm", "importbmlimit");
        log.info("# downloadSubjectAreaLimitTemplate configSecretKey:{}, secretKey", configSecretKey, secretKey);
        if(StrUtil.isBlankIfStr(configSecretKey) || StrUtil.isBlankIfStr(secretKey) ) {
            return ApiResponse.<String>builder().code(31451106).data(null).build();
        }
        if(!secretKey.equals(configSecretKey)) {
            return ApiResponse.<String>builder().code(31451106).data(null).build();
        }
        if(StrUtil.isEmptyIfStr(projectId)) {
            return ApiResponse.<String>builder().code(31451101).data(null).build();
        }
        Project project = projectMapper.selectById(projectId);
        if(null == project) {
            return ApiResponse.<String>builder().code(31451101).data(null).build();
        }
        String url = iSubjectAreaLimitService.downloadSubjectLimitTemplate(project);
        log.info("# downloadSubjectAreaLimitTemplate project:{}, url:{}", projectId, url);
        return ApiResponse.<String>builder().code(Constant.RESPONSE_CODE_SUCCESS).data(url).build();
    }
    
    
    @ApiOperation(value = "45.2.1 导入区域限额模板", notes = "20000:成功；45020101：导入区域限额模板失败")
    @ApiOperationSort(value = 2)
    @RequestMapping(value = "/importSubjectAreaLimitTemplate", method = RequestMethod.POST)
    @ThrowsAdvice(code = 31452101, print = "导入区域限额模板失败")
    @ApiVersion(group = BmApiVersionConstant.V2_4_p18)
    @ProgressAnno(total = 100, timeoutSeconds = 60 * 60 * 2, errorCode = 31452101, canConcurrent = true)
    public void importSubjectAreaLimitTemplate(MultipartFile file,  String projectId, String secretKey) throws Exception {
        log.info("# importSubjectAreaLimitTemplate project:{}", projectId);
        if(StrUtil.isEmptyIfStr(projectId)) {
            ProgressManager.updateProgressFailMessage(31452101, "项目ID不存在或者通过项目ID查询不到项目信息");
            return ;
        }
        String configSecretKey = iPtSecretKeyService.getSecretKey("fjzf", "bm", "importbmlimit");
        log.info("# importSubjectAreaLimitTemplate configSecretKey:{}, secretKey", configSecretKey, secretKey);
        if(StrUtil.isBlankIfStr(configSecretKey) || StrUtil.isBlankIfStr(secretKey) ) {
            ProgressManager.updateProgressFailMessage(31452108, "接口秘钥不正确");
            return ;
        }
        if(!secretKey.equals(configSecretKey)) {
            ProgressManager.updateProgressFailMessage(31452108, "接口秘钥不正确");
            return ;
        }
        Project project = projectMapper.selectById(projectId);
        if(null == project) {
            ProgressManager.updateProgressFailMessage(31452101, "项目ID不存在或者通过项目ID查询不到项目信息");
            return ;
        }
        try {
            iSubjectAreaLimitService.importSubjectAreaLimitTemplate(file, project);
        } catch (BusinessLogicException e) {
            log.error("# importSubjectAreaLimitTemplate error", e);
            ProgressManager.updateProgressFailMessage(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("# importSubjectAreaLimitTemplate error", e);
            ProgressManager.updateProgressFailMessage(31452101, "导入区域限额模板失败");
        }
    }
}
